package com.book.book.service.impl;

import com.book.book.dao.RulerDao;
import com.book.book.domain.ApplyDO;
import com.book.book.domain.RulerDO;
import com.book.common.utils.Query;
import com.book.iphone.domain.ImgUrlSplitUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.book.book.dao.BooksDao;
import com.book.book.domain.BooksDO;
import com.book.book.service.BooksService;



@SuppressWarnings("ALL")
@Service
public class BooksServiceImpl implements BooksService {
	@Autowired
	private BooksDao booksDao;
    @Autowired
    private RulerDao rulerDao;
	
	@Override
	public BooksDO get(Integer bookId){
		return booksDao.get(bookId);
	}
	
	@Override
	public List<BooksDO> list(Map<String, Object> map){
		return booksDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return booksDao.count(map);
	}
	
	@Override
	public int save(BooksDO books){
	    books.setUpDate(new Date());
	    books.setTotal(1);
	    //1.把规则内容插入到规则表，并且获取插入后该记录的主键
//        RulerDO rulerDO = new RulerDO();
//        rulerDao.insertReturnPK();
        //2.把主键插入商品（books）表中
		return booksDao.save(books);
	}
	
	@Override
	public int update(BooksDO books){

		return booksDao.update(books);
	}
	
	@Override
	public int remove(Integer bookId){
		return booksDao.remove(bookId);
	}
	
	@Override
	public int batchRemove(Integer[] bookIds){
		return booksDao.batchRemove(bookIds);
	}

	@Override
	public int insertSelective(ApplyDO apply) {
		//书名，作者，出版社 查看有无重复
		Map<String,Object> map = new HashMap();
		map.put("book_name",apply.getBookName().trim()==null?null:apply.getBookName().trim());
		map.put("author",apply.getAuthor().trim()==null?null:apply.getAuthor().trim());
		map.put("press",apply.getPress().trim()==null?null:apply.getPress().trim());
//		Query query = new Query(map);
		List<BooksDO> booksDOList = booksDao.list(map);
		if (booksDOList.size()>0){
			//已经存在这本书，只需要把数量加1 即可
			BooksDO booksDO = new BooksDO();
			booksDO.setBookId(booksDOList.get(0).getBookId());
			booksDO.setTotal(booksDOList.get(0).getTotal()+1);
			return booksDao.update(booksDO);
		}else if (booksDOList.size() == 0){
			//新书，执行插入操作
			BooksDO booksDO = new BooksDO();
			booksDO.setUserId(apply.getUserId()+"");
			booksDO.setBookName(apply.getBookName().trim());
			booksDO.setBookImg(apply.getBookImg());
			booksDO.setAuthor(apply.getAuthor().trim());
			booksDO.setPress(apply.getPress().trim());
			booksDO.setRulerId(apply.getRulerId());
			booksDO.setUpDate(new Date());
			return booksDao.save(booksDO);
		}else {
			return -1;
		}
	}


	/**
	 *根据用户ID 查询用户贡献过 的书
	 * @param userid
	 * @return
	 */
	@Override
	public List<BooksDO> getbooks(String userid) {
		List<BooksDO> booksDOList=booksDao.getbooks(userid);
		if (booksDOList.size()!=0) {
			for (int i=0;i<booksDOList.size();i++) {
				BooksDO booksDO=new BooksDO();
				booksDO=booksDOList.get(i);
				String [] imgs ;
				imgs=booksDO.getBookImg().split(",");
				if (imgs.length!=0){
					booksDO.setBookImg(imgs[0]);
				}
			}

		}
         return booksDOList;
	}

	@Override
	public List<BooksDO> listWithName(Map<String, Object> map) {
        return booksDao.listWithCatName(map);
	}


//	查询出数据库的图书本数
	@Override
	public List<BooksDO> list() {
		return null;
	}
}
